/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.ddtroops.serviceclasses;

import cz.muni.fi.pa165.ddtroops.daointerfaces.RaceDAO;
import cz.muni.fi.pa165.ddtroops.dto.DTOFactory;
import cz.muni.fi.pa165.ddtroops.dto.RaceDTO;
import cz.muni.fi.pa165.ddtroops.entities.EntityBuilder;
import cz.muni.fi.pa165.ddtroops.entities.Race;
import cz.muni.fi.pa165.ddtroops.serviceinterfaces.RaceService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Matěj
 */
@Service
@Qualifier("raceService")
public class RaceServiceImpl implements RaceService
{
    @Autowired(required=true)
    private RaceDAO dao;

    public void setDao(RaceDAO dao) {
        this.dao = dao;
    }
    
    @Transactional
    @Secured("ROLE_SUPERVISOR")
    public RaceDTO createNewRace(String name, String description, Byte strength, 
            Byte dexterity, Byte constitution, Byte intelligence, Byte wisdom, 
            Byte charisma) 
    {
        Race r =  new EntityBuilder<Race>(Race.class)
                .withProperty("Name", name)
                .withProperty("Description", description)
                .withProperty("Strength", (byte)(strength))
                .withProperty("Dexterity", (byte)(dexterity))
                .withProperty("Constitution", (byte)(constitution))
                .withProperty("Intelligence", (byte)(intelligence))
                .withProperty("Wisdom", (byte)(wisdom))
                .withProperty("Charisma", (byte)(charisma))
                .Build();
        
        return DTOFactory.createRaceDTO(r);
    }

    @Transactional
    @Secured("ROLE_SUPERVISOR")
    public void create(RaceDTO raceDTO) 
    {
        dao.create(DTOFactory.createRace(raceDTO));
    }

    @Transactional
    @Secured("ROLE_SUPERVISOR")
    public void update(RaceDTO raceDTO) 
    {
        dao.update(DTOFactory.createRace(raceDTO));
    }

    @Transactional
    @Secured("ROLE_SUPERVISOR")
    public void delete(RaceDTO raceDTO) 
    {
        dao.delete(DTOFactory.createRace(raceDTO));
    }

    @Transactional(readOnly=true)
    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    public RaceDTO getById(Long id) 
    {
        Race h = dao.getById(id);
        return (h!=null)?DTOFactory.createRaceDTO(h):null;
    }

    @Transactional(readOnly=true)
    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    public List<RaceDTO> getAll() 
    {     
        List<RaceDTO> raceDTOs = new ArrayList<RaceDTO>();
        for (Race r : dao.getAll())
                raceDTOs.add(DTOFactory.createRaceDTO(r));
        return raceDTOs;
    }
}
